package ywh.player
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import ywh.event.PlayerEvent;
	import ywh.list.CourseItem;
	import ywh.manager.Core;
	import ywh.manager.Tag;
	import ywh.utils.XMLUtils;
	
	/**
	 * 无时间轴展示器抽象类;
	 * 展示类调整呈现是依靠layoutSprite的setRectagle方法实现的;
	 * @author WilliWHYang
	 */
	public class AbstractShower
	{
		protected var _time:Number = NaN;
		protected var _duration:Number = NaN;
		
		protected var _state:String;
		protected var _using:Boolean;
		protected var _playItem:CourseItem;
		protected var _defalutConfig:XML;
		
		protected var _container:DisplayObjectContainer;
		protected var _layoutSprite:RelayoutSprite;
		protected var _render:DisplayObject;
		
		/**
		 * 属性不能重写，只能靠 get 方法重写;
		 * 本功能主要为播放器需要加载其它控件时准备；
		 * 如：AS2播放器;
		 * 需要注意，在播放器加载完成后需要及时抛出PlayerEvent.PLAYER_INIT
		 *
		 */
		protected function get createPlayerDirectly():Boolean
		{
			return true;
		}
		
		public function AbstractShower()
		{
		}
		
		//=========以下为构建播放器=====================
		/**
		 * 构建播放器;
		 * @param	defaultData 初始化player此信息来自init.xml
		 */
		public function initPlayer(config:XML):void
		{
			_defalutConfig = config;
			if (createPlayerDirectly)
			{
				dispatchEvent(PlayerEvent.PLAYER_INIT);
			}
			_defalutConfig.hasOwnProperty(Tag._CONTAINER) && (initLayout(), initDisplayer());
		}
		
		//================呈现控制的相关方法============================================
		/**
		 * 子类需覆盖此方法;
		 * 初始化Player的呈现器;
		 */
		protected function initLayout():void
		{
			var containerName:String = XMLUtils.getPropString(_defalutConfig, Tag._CONTAINER);
			var layoutSpriteName:String = XMLUtils.getPropString(_defalutConfig, Tag._LAYOUT_SPRITE);
			if (layoutSpriteName)
			{
				_container = Core.getDisplayObject(containerName) as DisplayObjectContainer;
				var tempDisplayer:DisplayObject = _container[layoutSpriteName] as DisplayObject;
				if (tempDisplayer is RelayoutSprite)
				{
					_layoutSprite = tempDisplayer as RelayoutSprite;
				}
				else
				{
					_render = tempDisplayer;
					_layoutSprite = new RelayoutSprite();
					_layoutSprite.bandRender = _render;
				}
			}
		
		}
		
		protected function initDisplayer():void
		{
		
		}
		
		public function get layoutSprite():RelayoutSprite
		{
			return _layoutSprite;
		}
		
		public function get render():DisplayObject
		{
			
			return _render;
		}
		
		public function set render(value:DisplayObject):void
		{
			_render = value;
			_layoutSprite.bandRender = _render;
		
		}
		
		/**
		 * 如果Player是项目列表或是菜单，缩略图列表等信息，需要设置本值，显示初始化信息;
		 */
		public function set dataProvider(data:XML):void
		{
		}
		
		//==================以下为播放器在播放期间的状态========================
		/**
		 * 正在播放的项目;
		 */
		public function set playItem(item:CourseItem):void
		{
			if (state != PlayerState.EMPTY)
			{
				emptyPlayItem();
			}
			state = PlayerState.LOADING;
			_using = true;
			_playItem = item;
			_pChangeItem();
		}
		
		public function get playItem():CourseItem
		{
			return _playItem;
		}
		
		/**
		 * 预同步处理;
		 * @return
		 */
		public function preSyncHandle():void
		{
			_time = 1;
			state = PlayerState.PAUSED;
		}
		
		/**
		 * 清空播放器;
		 */
		public function emptyPlayItem():void
		{
			_pDestroy();
			_playItem = null;
			_using = false;
			_duration = NaN;
			_time = NaN;
			state = PlayerState.EMPTY;
		}
		
		/**
		 * 获取项目时长;
		 */
		public function get duration():Number
		{
			return isNaN(_duration) ? ((_playItem) ? _duration = _playItem.duration : NaN) : _duration;
		}
		
		/**
		 * 播放器播放状态;
		 */
		public function get state():String
		{
			return _state;
		}
		
		public function set state(value:String):void
		{
			if (value != _state)
			{
				_state = value;
				dispatchEvent(PlayerEvent.PLAYER_CHANGE_STATE);
			}
		}
		
		/**
		 * 播放器是否正在使用;
		 */
		public function get using():Boolean
		{
			return _using;
		}
		
		protected function dispatchEvent(type:String):void
		{
			Core.dispatchEvent(new PlayerEvent(type, this));
		}
		
		//=====================以下为播放器特异性方法需要覆盖========================================
		/**
		 * [播放器特异性方法需要覆盖]用以获取播放器状态;
		 * @return
		 */
		protected function get _pState():String
		{
			return null;
		}
		
		/**
		 * [播放器特异性方法需要覆盖]在set playItem时调用;
		 */
		protected function _pChangeItem():void
		{
		}
		
		/**
		 * [播放器特异性方法需要覆盖]在Destroy()时调用;
		 */
		protected function _pDestroy():void
		{
		}
		
		//=====================以下为具有时间线的Player需要的方法;========================================
		public function get time():Number
		{
			return _time;
		}
		
		public function seekTo(value:Number):void
		{
			return;
		}
		
		public function play():void
		{
			return;
		}
		
		public function pause():void
		{
			return;
		}
		
		protected function canSeekTo(value:Number):Boolean
		{
			return false;
		}
	
	}

}